package com.story.modular.app.controller;

import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.ChannelMapper;
import com.story.common.persistence.model.Channel;
import com.story.core.base.controller.BaseController;
import com.story.core.util.ToolUtil;
import com.story.modular.app.dao.ChannelDao;
import com.story.modular.app.dao.ChannelItemDao;
import com.story.modular.app.dao.UserChannelDao;
import com.story.modular.app.warpper.CategoryWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yangbo
 * @date 2019-08-05 19:51
 */
@Controller
@RequestMapping(value = "/channel")
public class ChannelController extends BaseController {

    public static final String PREFIX = "/app/channel/";

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private UserChannelDao userChannelDao;

    @Autowired
    private ChannelItemDao channelItemDao;

    /**
     * 进入专辑列表页
     * @return
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "channel.html";
    }

    /**
     * 列表数据
     * @param condition
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String,Object>> channelList = channelDao.getChannelList(condition);
        return super.warpObject(new CategoryWarpper(channelList));
    }

    /**
     * 新增页面
     * @return
     */
    @RequestMapping(value = "/channelAdd")
    public String add() {
        return PREFIX + "channel_add.html";
    }

    /**
     * 新增专频道
     * @param channel
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Channel channel) {
        if(ToolUtil.isOneEmpty(channel, channel.getTitle())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }

        int count = channelDao.countByTitle(channel.getTitle().trim());
        if(count > 0) {
            throw new BussinessException(BizExceptionEnum.CATE_NAME_EXIST);
        }
        channelMapper.insert(channel);
        return super.SUCCESS_TIP;
    }

    /**
     * 修改页面返回
     * @param channelId
     * @param model
     * @return
     */
    @RequestMapping(value = "/channelUpdate/{channelId}")
    public String updateCategory(@PathVariable(value = "channelId") Integer channelId,
                                 Model model) {
        Channel channel = channelMapper.selectById(channelId);
        model.addAttribute("channel",channel);
        return PREFIX + "channel_update.html";
    }

    /**
     * 修改专辑
     * @param channel
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object updateCategory(Channel channel) {
        if(ToolUtil.isOneEmpty(channel,channel.getId(),channel.getTitle())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        channelMapper.updateById(channel);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除专辑
     * @param channelIds
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object deleteCategory(String channelIds) {
        List<Integer> channelIdList = new ArrayList<>();
        String[] channelIdArr = channelIds.split(",");
        for (String channelId : channelIdArr) {
            channelIdList.add(Integer.valueOf(channelId));
        }
        //解绑频道与音频之间的关系
        channelItemDao.batchUnbindRelationByChannelId(channelIdList);
        //解绑频道跟用户之间的关系
        userChannelDao.batchUnbindRelationByChannelId(channelIdList);
        //删除频道
        channelDao.batchDeleteById(channelIdList);
        return super.SUCCESS_TIP;
    }

}
